﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using S4Store.Models.TelSaleReport;
using S4Store.Service;
using S4Store.Models;
using S4Store.Domain;
using S4Store.Models.Chart;

namespace S4Store.Controllers
{
    public class TelSaleReportController : ReportBaseController
    {
        public TelSaleReportService TelSaleReportService { get; set; }
        public TelSaleKpiService TelSaleKpiService { get; set; }
        public CarService CarService { get; set; }
        public SellerKpiService SellerKpiService { get; set; }

        public ReportService ReportService { get; set; }
        public EmployeeService SellerService { get; set; }
        public CarKpiService CarKpiService { get; set; }
        //public s
        public ActionResult Index()
        {
            IndexModel model = new IndexModel();
            using (var t = new Transtion())
            {
                t.Inject(TelSaleReportService);
                t.Inject(TelSaleKpiService);
                var carbrands=TelSaleKpiService.GetCarbrandsForStoreMonth(Month.Year,Month.Month,Store.Id).ToList();
                foreach (var carbrand in carbrands)
                {
                    var report = TelSaleReportService.GetCarBrandReport(Month.Year, Month.Month, Store.Id, carbrand.Id);
                    model.Reports.Add(report);
                }               
                t.Commit();
            }

            this.InjectReportModel(model);
            model.BarModel.Action = "Index";
            model.BarModel.Controller = "TelSaleReport";
            model.BarModel.AddView(ReportBarModel.ViewItem.Month);
            return View(model);
        }

        public ActionResult CarBrandDetail()
        {
            CommonModel model = new CommonModel();
            using (var t = new Transtion())
            {
                t.Inject(TelSaleReportService);
                t.Inject(ReportService);
                model.Standard = ReportService.GetSellerPerformanceStandard(Month.Year, Month.Month, Store.Id);
                model.Report = TelSaleReportService.GetCarBrandReport(Month.Year, Month.Month, Store.Id, SearchCarBrandId.Value);
                t.Commit();
            }

            this.InjectReportModel(model);
            model.BarModel.Action = "CarBrandDetail";
            model.BarModel.Controller = "TelSaleReport";
            model.BarModel.AddView(ReportBarModel.ViewItem.Month);
            model.BarModel.RemoveView(ReportBarModel.ViewItem.Stores);
            model.BarModel.PlusItem = "<a target='navtab' class='historyrecord' title='" + model.Report.CarBrand.Name + "电话销售历史数据查询' href='" + Url.Action("CarBrandHistory", new { SearchCarBrandId = SearchCarBrandId }) + "' ><span>历史数据查询</span></a>";
            return View(model);
        }

        public ActionResult CarBrandHistory()
        {
            CarBrandHistoryModel model = new CarBrandHistoryModel();
            if (Start == null)
            {
                Start = DateTime.Now.AddMonths(-6);
                End = DateTime.Now;
            }

            using (var t = new Transtion())
            {
                t.Inject(TelSaleReportService);
                t.Inject(CarService);
                for (DateTime dt = Start.Value; dt <= End; dt = dt.AddMonths(1))
                {
                    var report = TelSaleReportService.GetCarBrandReport(dt.Year, dt.Month, Store.Id, SearchCarBrandId.Value);
                    model.Reports.Add(report);
                }
                model.CarBrand = CarService.GetCarBrandQuery().FirstOrDefault(x => x.Id == SearchCarBrandId.Value);
                t.Commit();
            }
            this.InjectReportModel(model);
            model.BarModel.Action = "CarBrandHistory";
            model.BarModel.Controller = "TelSaleReport";
            model.BarModel.AddView(ReportBarModel.ViewItem.StartEnd);
            model.Start = Start.Value;
            model.End = End.Value;
            return View(model);
        }

        public ActionResult TelSellerIndex()
        {
            TelSellerIndexModel model = new TelSellerIndexModel();
            using (var t = new Transtion())
            {
                t.Inject(ReportService);
                var report = ReportService.GetSellerReport(Month.Year, Month.Month, Store.Id, EmployeeType.TelSaler);
                model.Report = report;
                t.Commit();
            }
            this.InjectReportModel(model);
            model.BarModel.Action = "TelSellerIndex";
            model.BarModel.Controller = "TelSaleReport";
            model.BarModel.AddView(ReportBarModel.ViewItem.Month);
            model.BarModel.PlusItem = "<a target='navtab' class='historyrecord' title='销售顾问执行过程KPI' href='" + Url.Action("BySeller") + "' ><span>人员查询</span></a>";
            return View(model);
        }




        public ActionResult SellerDataItem(SellerDataItemType type)
        {
            SellerDataItemModel model = new SellerDataItemModel();
            if (Start == null)
            {
                Start = DateTime.Now.AddMonths(-6);
            }
            if(End==null)
                End = DateTime.Now;

            using (var t = new Transtion())
            {
                t.Inject(ReportService);
                t.Inject(SellerKpiService);
                model.ItemType = type;
                model.IsMonthConfigConfirmed = SellerKpiService.IsMonthConfiged(Month.Year, Month.Month, Store.Id);
                if (model.IsMonthConfigConfirmed)
                {
                    foreach (var seller in Store.GetEnbleEmployee(EmployeeType.TelSaler))
                    {
                        var report = ReportService.GetSellerPersonReport(Month.Year, Month.Month, Store.Id, seller.Id);

                        model.SellerPersonReports.Add(report);
                    }
                }
                model.SellerReportModel = ReportService.GetSellerReport(Month.Year, Month.Month, Store.Id,EmployeeType.TelSaler);
                t.Commit();
            }

            model.LineModel = getLine(type, Start.Value, End.Value, Store.Id);
            model.LineModel.Colors = new List<string> { S4Store.Models.Chart.Color.Green, S4Store.Models.Chart.Color.Blue };
            this.InjectReportModel(model);
            model.BarModel.Action = "SellerDataItem";
            model.BarModel.Controller = "TelSaleReport";
            model.BarModel.PlusItem = "<input type='hidden' name='type' value='" + type.ToInt() + "'/>";
            model.BarModel.AddView(ReportBarModel.ViewItem.StartEnd);
            model.BarModel.RemoveView(ReportBarModel.ViewItem.Month);
            model.BarModel.RemoveView(ReportBarModel.ViewItem.Stores);
            return View(model);
        }

        private Line getLine(SellerDataItemType type, DateTime start, DateTime end, int storeid)
        {
            Line model = new Line();
            model.YAxis = "比例值(%)";
            model.Name = "销售部半年平均" + type.ToString() + "走势";
            model.XAxis = getXaxis(type, start, end);
            model.Serieses = getSerieses(type, start, end, storeid);
            return model;
        }

        private List<ChartSeries> getSerieses(SellerDataItemType type, DateTime start, DateTime end, int storeid)
        {
            var series = new List<ChartSeries>();
            var smb = new ChartSeries();
            smb.Data = new List<decimal>();
            var ssj = new ChartSeries();
            ssj.Data = new List<decimal>();
            switch (type)
            {
                case SellerDataItemType.电话招徕成功率:
                    {
                        smb.Name = "电话招徕到店率目标";
                        ssj.Name = "实际电话招徕率";
                        using (var t = new Transtion())
                        {
                            t.Inject(ReportService);
                            for (DateTime dt = start; dt <= end; dt = dt.AddMonths(1))
                            {
                                var report = ReportService.GetSellerReport(dt.Year, dt.Month, storeid, EmployeeType.TelSaler);
                                smb.Data.Add(report.SellerPerformanceStandard.DHZLDDLV);
                                ssj.Data.Add(report.SCLDDJLV);
                            }
                            t.Commit();
                        }
                        break;
                    }
                case SellerDataItemType.试乘试驾率:
                    {
                        smb.Name = "目标试乘试驾率";
                        ssj.Name = "实际试乘试驾率";
                        using (var t = new Transtion())
                        {
                            t.Inject(ReportService);
                            for (DateTime dt = start; dt <= end; dt = dt.AddMonths(1))
                            {
                                var report = ReportService.GetSellerReport(dt.Year, dt.Month, storeid, EmployeeType.TelSaler);
                                smb.Data.Add(report.SellerPerformanceStandard.SCSJLV);
                                ssj.Data.Add(report.SCSJLV);
                            }
                            t.Commit();
                        }
                        break;
                    }
                case SellerDataItemType.试驾未成交率:
                    {
                        smb.Name = "目标试驾未成交率";
                        ssj.Name = "实际试驾未成交率";
                        using (var t = new Transtion())
                        {
                            t.Inject(ReportService);
                            for (DateTime dt = start; dt <= end; dt = dt.AddMonths(1))
                            {
                                var report = ReportService.GetSellerReport(dt.Year, dt.Month, storeid, EmployeeType.TelSaler);
                                smb.Data.Add(report.SellerPerformanceStandard.SCSJCJLV);
                                ssj.Data.Add(report.SCSJCJLV);
                            }
                            t.Commit();
                        }
                        break;
                    }
                case SellerDataItemType.登记客户成交率:
                    {
                        smb.Name = "目标登记客户成交率";
                        ssj.Name = "实际登记客户成交率";
                        using (var t = new Transtion())
                        {
                            t.Inject(ReportService);
                            for (DateTime dt = start; dt <= end; dt = dt.AddMonths(1))
                            {
                                var report = ReportService.GetSellerReport(dt.Year, dt.Month, storeid, EmployeeType.TelSaler);
                                smb.Data.Add(report.SellerPerformanceStandard.DJKHCJLV);
                                ssj.Data.Add(report.DJKHCJLV);
                            }
                            t.Commit();
                        }
                        break;
                    }
                case SellerDataItemType.上月客户转化率:
                    {
                        smb.Name = "目标上月客户转化率";
                        ssj.Name = "实际上月客户转化率";
                        using (var t = new Transtion())
                        {
                            t.Inject(ReportService);
                            for (DateTime dt = start; dt <= end; dt = dt.AddMonths(1))
                            {
                                var report = ReportService.GetSellerReport(dt.Year, dt.Month, storeid, EmployeeType.TelSaler);
                                smb.Data.Add(report.SellerPerformanceStandard.SYKHZHLV);
                                ssj.Data.Add(report.SYKHZHLV);
                            }
                            t.Commit();
                        }
                        break;
                    }
                case SellerDataItemType.超限价格成交率:
                    {
                        smb.Name = "目标超限率";
                        ssj.Name = "实际超限率";
                        using (var t = new Transtion())
                        {
                            t.Inject(ReportService);
                            for (DateTime dt = start; dt <= end; dt = dt.AddMonths(1))
                            {
                                var report = ReportService.GetSellerReport(dt.Year, dt.Month, storeid, EmployeeType.TelSaler);
                                smb.Data.Add(report.SellerPerformanceStandard.CXJGCJLV);
                                ssj.Data.Add(report.CXJGCJLV);
                            }
                            t.Commit();
                        }
                        break;
                    }
            }
            series.Add(smb);
            series.Add(ssj);
            return series;
        }

        private List<String> getXaxis(SellerDataItemType type,  DateTime start, DateTime end)
        {
            List<String> axis = new List<string>();
            for (DateTime dt = start; dt <= end; dt = dt.AddMonths(1))
            {
                axis.Add(dt.ToString("yy-M"));
            }
            return axis;
        }

        public ActionResult BySeller()
        {
            BySellerModel model = new BySellerModel();
            model.ChartData.Name = "销售顾问执行过程KPI";
            model.ChartData.YAxis = "进度百分比(%)";
            model.ChartData.Colors = new List<string> { S4Store.Models.Chart.Color.Green, S4Store.Models.Chart.Color.Blue };
            var seriestime = new ChartSeries();
            seriestime.Name = "时间进度";
            var seriessale = new ChartSeries();
            seriessale.Name = "销量进度";
            model.ChartData.Serieses.Add(seriestime);
            model.ChartData.Serieses.Add(seriessale);
            using (var t = new Transtion())
            {
                t.Inject(ReportService);
                t.Inject(SellerKpiService);
                model.IsMonthConfigConfirmed = SellerKpiService.IsMonthConfiged(Month.Year, Month.Month, Store.Id);
                foreach (var seller in Store.GetEnbleEmployee(EmployeeType.TelSaler))
                {
                    model.ChartData.XAxis.Add(seller.Name);
                    var report = ReportService.GetSellerPersonReport(Month.Year, Month.Month, Store.Id, seller.Id);
                    seriestime.Data.Add(report.RateOfTime);
                    seriessale.Data.Add(report.SaleRate);

                    //差值
                    var value = (report.SaleCount - report.SaleTarget * report.RateOfTime / 100).AutoCeling();
                    //RedGreenIndex index = new RedGreenIndex();
                    //index.Size = RedGreenIndex.IndexSize.Middle;
                    //if (value >= 0)
                    //{
                    //    index.Type = RedGreenIndex.IndexType.Green;
                    //}
                    //else
                    //{
                    //    index.Type = RedGreenIndex.IndexType.Red;
                    //    index.Value = ((int)value).ToString();
                    //}
                    //index.Title = seller.Name + " 销量时间进度差过程KPI";
                    //index.Url = Url.Action("ByPerson", new { SearchSellerId = seller.Id });
                    //model.RedGreenIndexs.Add(index);

                    AdvIndex idx = new AdvIndex();
                    idx.Item1Name = "实际订单总数";
                    idx.Item1Value = report.OrderCount.ToString();
                    idx.Item2Name = "实际交车总数";
                    idx.Item2Value = report.SaleCount.ToString();
                    idx.Size = AdvIndex.IndexSize.Middle;
                    idx.Tip = seller.Name + "销售执行过程KPI";
                    idx.Title = "实际交车总数与目标销量的差值";
                    idx.Type = (value >= 0 ? AdvIndex.IndexType.Green : AdvIndex.IndexType.Red);
                    idx.Url = Url.Action("ByPerson", new { SearchSellerId = seller.Id });
                    idx.Value = value.ToString(CultureInfo.InvariantCulture);
                    model.AdvIndexs.Add(idx);
                }
                t.Commit();
            }
            this.InjectReportModel(model);
            model.BarModel.Action = "BySeller";
            model.BarModel.Controller = "TelSaleReport";
            model.BarModel.AddView(ReportBarModel.ViewItem.Month);
            return View(model);
        }

        public ActionResult ByPerson()
        {
            ByPersonModel model = new ByPersonModel();
            using (var t = new Transtion())
            {
                t.Inject(ReportService);
                t.Inject(SellerService);
                t.Inject(SellerKpiService);
                t.Inject(CarKpiService);
                model.Seller = SellerService.GetQuery().FirstOrDefault(x => x.Id == SearchSellerId.Value);
                model.IsMonthConfigConfirmed = SellerKpiService.IsMonthConfiged(Month.Year, Month.Month, Store.Id);
                //foreach (var seller in Store.EnbleSellers)
                //{
                var brands = CarKpiService.GetCarbrandsForStoreMonth(Month.Year, Month.Month, Store.Id).ToList();
                foreach (var brand in brands)
                {
                    
                    var report = ReportService.GetSellerPersonBrandReport(Month.Year, Month.Month, Store.Id, SearchSellerId.Value, brand.Id);
                    report.Seller = model.Seller;
                    report.CarBrand = brand;
                    model.SellerKpiPersonBodyModel.Reports.Add(report);
                }
                model.SellerKpiPersonBodyModel.PersonReport = ReportService.GetSellerPersonReport(Month.Year, Month.Month, Store.Id, SearchSellerId.Value);
                model.SellerKpiPersonBodyModel.StoreReport = ReportService.GetSellerReport(Month.Year, Month.Month, Store.Id, EmployeeType.TelSaler);
                //}
                t.Commit();
            }
            this.InjectReportModel(model);
            model.BarModel.Action = "ByPerson";
            model.BarModel.Controller = "TelSaleReport";
            model.BarModel.AddView(ReportBarModel.ViewItem.Month);
            return View(model);
        }

        public ActionResult Customer()
        {
            HistoryModel model = new HistoryModel();
            if (Start == null)
            {
                Start = DateTime.Now.AddMonths(-6);
                End = DateTime.Now;
            }

            using (var t = new Transtion())
            {
                t.Inject(ReportService);
                t.Inject(SellerService);
                t.Inject(SellerKpiService);
                t.Inject(CarKpiService);

                model.Seller = SellerService.GetQuery().FirstOrDefault(x => x.Id == SearchSellerId.Value);
                for (DateTime dt = Start.Value; dt <= End; dt = dt.AddMonths(1))
                {
                    var report = ReportService.GetSellerPersonReport(dt.Year, dt.Month, Store.Id, SearchSellerId.Value);
                    report.Seller = model.Seller;
                    model.Reports.Add(report);
                }
                t.Commit();
            }
            this.InjectReportModel(model);
            model.BarModel.Action = "Customer";
            model.BarModel.Controller = "TelSaleReport";
            model.BarModel.AddView(ReportBarModel.ViewItem.StartEnd);
            model.BarModel.RemoveView(ReportBarModel.ViewItem.Stores);
            return View(model);
        }
    
    }
}
